<template>
  <div class="app-container">
    <el-card class="box-card main">
      <div slot="header" class="clearfix">
        <span>新增参会企业</span>
        <el-button
          style="float: right; padding: 0;margin-left:14px"
          type="text"
          @click="goto('/jobfair/exhibitors/list')"
        >
          返回
        </el-button>
        <el-button
          style="float: right; padding: 0;"
          type="text"
          :disabled="issubmit"
          @click="onSubmit('form')"
        >
          保存
        </el-button>
      </div>
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tbody><tr>
          <td width="500" valign="top">
            <el-form
              ref="form"
              class="common-form"
              :model="form"
              label-width="120px"
              :rules="rules"
              :inline-message="true"
            >
              <div class="searchKey">
                <el-form-item class="w400" label="企业名称">
                  <el-input v-model="companyname" @input="onCompany('companyname')" />
                </el-form-item>
                <el-form-item class="w400" label="或会员ID">
                  <el-input v-model="uid" @input="onCompany('uid')" />
                </el-form-item>
              </div>
              <el-form-item label="预定方式" prop="etype">
                <el-radio-group v-model="form.etype">
                  <el-radio :label="1">在线</el-radio>
                  <el-radio :label="2">电话</el-radio>
                  <el-radio :label="3">其它方式</el-radio>
                </el-radio-group>
              </el-form-item>
              <!--              <el-form-item label="预定状态" prop="audit">-->
              <!--                <el-radio-group v-model="form.audit">-->
              <!--                  <el-radio :label="1">成功预定</el-radio>-->
              <!--                  <el-radio :label="2">等待确认</el-radio>-->
              <!--                </el-radio-group>-->
              <!--              </el-form-item>-->
              <el-form-item label="预定招聘会" prop="jobfair_id">
                <el-select v-model="form.jobfair_id" placeholder="选择招聘会" @change="onPosition">
                  <el-option
                    v-for="(item, index) in jobfairList"
                    :key="index"
                    :label="item.title"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="预定展位" prop="position_id">
                <el-select v-model="form.position_id" placeholder="选择展位">
                  <el-option
                    v-for="(item, index) in position"
                    :key="index"
                    :label="item.position"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="推荐名企" prop="recommend">
                <el-radio-group v-model="form.recommend">
                  <el-radio :label="1">推荐</el-radio>
                  <el-radio :label="0">未推荐</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="备注" prop="note" class="w460">
                <el-input v-model="form.note" type="textarea" rows="4" />
              </el-form-item>
              <el-form-item label="">
                <el-button type="primary" :disabled="issubmit" @click="onSubmit('form')">保存</el-button>
                <el-button @click="goto('/jobfair/exhibitors/list')">返回</el-button>
              </el-form-item>
            </el-form>
          </td>
          <td valign="top">
            <el-table
              v-if="list.length>0"
              v-loading="listLoading"
              class="companyWrap"
              :data="list"
              element-loading-text="Loading"
              fit
              highlight-current-row
            >
              <el-table-column width="30">
                <template slot-scope="scope">
                  <label><input v-model="form.comid" type="radio" class="comid" name="comid" :value="scope.row.id"></label>
                </template>
              </el-table-column>
              <el-table-column label="公司名称" show-overflow-tooltip>
                <template slot-scope="scope">
                  <el-link :href="scope.row.company_link" target="_blank" type="primary">
                    {{ scope.row.companyname }}
                  </el-link>
                </template>
              </el-table-column>
              <el-table-column align="center" prop="created_at" label="刷新时间" width="200">
                <template slot-scope="scope">
                  <i class="el-icon-time" />
                  <span>{{ scope.row.refreshtime | timeFilter }}</span>
                </template>
              </el-table-column>
              <el-table-column align="center" prop="created_at" label="添加时间" width="200">
                <template slot-scope="scope">
                  <i class="el-icon-time" />
                  <span>{{ scope.row.addtime | timeFilter }}</span>
                </template>
              </el-table-column>
            </el-table>
            <div v-if="list.length<=0" class="companyWrap companyNo">没有找到对应的公司。</div>
          </td>
        </tr></tbody>
      </table>
    </el-card>
  </div>
</template>

<script>
import { parseTime } from '@/utils/index'
import { jobfairListAll, jobfairCompanySearch, getPosition, exhibitorsAdd } from '@/api/jobfair'

export default {
  filters: {
    timeFilter(timestamp) {
      return parseTime(timestamp, '{y}-{m}-{d} {h}:{i}')
    }
  },
  data() {
    return {
      companyname: '',
      uid: '',
      jobfairList: [],
      form: {
        etype: 1,
        // audit: 1,
        recommend: 1,
        jobfair_id: '',
        position_id: '',
        note: '',
        comid: ''
      },
      issubmit: false,
      key: '',
      type: '',
      position: [],
      list: [],
      listLoading: !1,
      rules: {
        jobfair_id: [
          { required: true, message: '请选择招聘会', trigger: 'blur' },
          { type: 'number', message: '请正确选择招聘会', trigger: 'blur' }
        ],
        position_id: [
          { required: true, message: '请选择展位', trigger: 'blur' },
          { type: 'number', message: '请正确选择展位', trigger: 'blur' }
        ],
        etype: [
          { type: 'number', message: '请正确选择预定方式', trigger: 'blur' }
        ],
        // audit: [
        //   { type: 'number', message: '请正确选择预定状态', trigger: 'blur' }
        // ],
        recommend: [
          { type: 'number', message: '请正确选择推荐名企', trigger: 'blur' }
        ],
        note: [
          { max: 200, message: '长度在 200 个字符内', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    config() {
      return this.$store.state.config
    }
  },
  created() {
    this.getJobfair()
  },
  methods: {
    getJobfair: function(){
      var t = this
      jobfairListAll().then(res => {
        t.jobfairList = res.data.items
      })
    },
    onCompany: function(t){
      const param = {
        key: this[t],
        type: t
      }
      this.form.comid = ''
      if (param.key == ''){
        this.list = []
      } else {
        this.listLoading = true
        jobfairCompanySearch(param).then(res => {
          this.list = res.data.items
          this.listLoading = false
        })
      }
    },
    onPosition: function(){
      const param = {
        jobfair_id: this.form.jobfair_id
      }
      this.form.position_id = ''
      getPosition(param).then(res => {
        this.position = res.data.items
      })
    },
    onSubmit(formName) {
      this.issubmit = true
      const that = this
      var insertData = Object.assign({}, this.form)
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (!insertData.comid){
            this.$message.error('请选择企业')
            return !1
          }
          exhibitorsAdd(insertData).then(response => {
            this.$message.success(response.message)
            setTimeout(function() {
              that.$router.push('/jobfair/exhibitors/list')
            }, 1500)
            return true
          }).catch(() => {
            that.issubmit = false
          })
        } else {
          that.issubmit = false
          return false
        }
      })
    },
    goto(target) {
      this.$router.push(target)
    }
  }
}
</script>
<style scoped>
.w400{width:400px}
.w460{width:460px}
.searchKey{background:#f5f7f8;padding:24px 0 2px;border:1px solid #dfdfdf;border-right:0}
.companyWrap{border:1px solid #dfdfdf}
.companyNo{padding:66px 20px;color:#0066CC}
</style>
